﻿namespace MapleRock.P2PEmulator.Core
{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;

  internal class PeersIspDistanceTable
  {
    public static readonly PeersIspDistanceTable Instance = new PeersIspDistanceTable();

    private Dictionary<uint, double> table = null;

    private uint currentId = 0;

    public double GetDistance(uint idA, uint idB)
    {
      Debug.Assert(this.currentId == idA, "this.currentId == idA");

      if (idA == idB)
      {
        return 0;
      }
      
      return this.table[idB];
    }

    public void ResetTable(IEnumerable<Peer> peers, Peer basePeer)
    {
      this.currentId = basePeer.Id;

      this.table = new Dictionary<uint, double>();
      foreach(var peer in peers)
      {
        var distance = Geometry.EvaluateDistance(basePeer.AbsolutePosition, peer.AbsolutePosition);
        this.table[peer.Id] = distance;
      }
    }
  }
}
